﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ModelExamples;

namespace SquatSimulator
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class MainForm : Window
  {
    Emulator _emulator = new Emulator(@"D:\CAB\Container_ship_23.cab");
    //Emulator _emulator;
    long _startTicks;
    const int convert_ticks_to_miliseconds = 10 * 1000;
    ShipData _shipData;

    public MainForm()
    {
      InitializeComponent();

      _shipData = new ShipData(_emulator);
      DataContext = _shipData;
      
      System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
      dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

      dispatcherTimer.Interval = new TimeSpan(100 * convert_ticks_to_miliseconds);
      dispatcherTimer.Start();

      _startTicks = DateTime.Now.Ticks;
      _emulator.Start(0);
      _shipData.Order = _emulator.EngineOrderState;
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
      double timeRatio = 1.0;

      var curTicks = DateTime.Now.Ticks - _startTicks;
      double dblDeltaTimeInSeconds =
        (double)curTicks / convert_ticks_to_miliseconds / 1000;
      _emulator.Run(timeRatio * dblDeltaTimeInSeconds);
      _shipData.Refresh(_emulator);
    }
  }
}
